<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('Ecatalogue')) return;
/**
 * EcataloguE
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage ecatalogue
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/EcataloguE
 */

/**
 *  Ecatalogue main class
 *  All common elements are located here  
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com) 
 */ 
class Ecatalogue {   
	/**
     * ProductModel
     */
    private $product_mdl;
	
	/**
     * PdkArticleModel
     */ 
    private $article_mdl;
    
    public function __construct() {
		$this->product_mdl = pdk_load_class('ProductModel','classes');
		$this->article_mdl = pdk_load_class('PdkArticleModel','classes');
    }
    
/**
 *  Checks whether given rubrique is EcataloguE products rubrique
 *
 * @param int $id_rubrique ID rubrique
 * @return bool
**/
public function IsEcatalogueRubrique($id_rubrique) { 
	static $rubs = array();
	static $rubs_catalogue = false;

	// deja trouve
	if (isset($rubs[$id_rubrique])) {
		return $rubs[$id_rubrique];
	}

	// init
	if ($rubs_catalogue === false) {
		$r = lire_config('ecatalogue/branches');
		include_spip('spip_bonux_fonctions');
		$rubs_catalogue = picker_selected($r, 'rubrique'); // fn de spip bonux
	}

	// trouve
	if (in_array($id_rubrique, $rubs_catalogue)) {
		$rubs[$id_rubrique] = true;
		return true;
	}

	// rechercher dans la parente
	if ($id_parent = sql_getfetsel('id_parent', 'spip_rubriques', 'id_rubrique='.$id_rubrique)) {
		 $rubs[$id_rubrique] = $this->IsEcatalogueRubrique($id_parent);
		 return $rubs[$id_rubrique];
	}

	// perdu :)
	return false;
}

/**
 *  Returns id of first found ecatalogue rubrique
 */
public function FindFirstRubriqueId() {
	$r = lire_config('ecatalogue/branches');
	include_spip('spip_bonux_fonctions');
	$rubs_catalogue = picker_selected($r, 'rubrique');
	return isset($rubs_catalogue[0]) ? $rubs_catalogue[0] : false;
}

/**
 *  Performs EcatalogE maintenance. It includes:
 *  - Deletion of all forgotten rows in spip_ecatalogue_products (products, articles for wich doesn't exist anymore)
 *  - Creating of products for articles in ecatalogue rubriques if they (articles) don't have them yet
 *
 *  @param array $log Log of performed operaions
 */
public function PerformMaintenance(&$log = array()) {
	$ok = true;

	// Delete all forgotten products
	$products = $this->product_mdl->select();
	$deleted_num = 0;
	
	foreach ($products as $product) {
		if (!$this->article_mdl->select_by_id($product['id_article'])) {
			if ($this->product_mdl->delete_by_id($product['id_article']))
				$deleted_num++;
			else
				$ok = false;
		}
	}
	$log[] = _T('ecatalogue:maintenance_num_rows_deleted_from_table', array('num_rows'=>$deleted_num, 'table'=>$this->product_mdl->main_table));
	
	// Add products for ecatalogue articles
	$articles = $this->article_mdl->select();
	$added_num = 0;
	foreach ($articles as $article) {
		if ($this->IsEcatalogueRubrique($article['id_rubrique']) && !$this->product_mdl->select_by_id($article['id_article'])) {
			$data = array('id_article'=>$article['id_article']);
			if ($this->product_mdl->insert($data))
				$added_num++;
			else
				$ok = false;
		}
	}	
	$log[] = _T('ecatalogue:maintenance_num_rows_added_into_table', array('num_rows'=>$added_num, 'table'=>$this->product_mdl->main_table));
	
	return $ok;
}

}
?>